package com.example.treehole.ui.page.postDetail

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.material.icons.filled.ThumbUpOffAlt
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.treehole.LocalUserViewModel
import com.example.treehole.service.DataRepository
import com.example.treehole.ui.component.formatTime
import com.example.treehole.ui.model.CommentToPost
import com.example.treehole.ui.model.CommentType
import com.example.treehole.ui.theme.iconBlue
import com.example.treehole.ui.theme.mediumDarkGray
import kotlinx.coroutines.launch

@Composable
fun PostCommentCard(
    commentToPost: CommentToPost,
    userName: String
) {
    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()
    val scope = rememberCoroutineScope()

    val createCommentViewModel = LocalCreateCommentViewModel.current

    val timeText = formatTime(commentToPost.cmtTime)
    val isLike = remember { mutableStateOf(commentToPost.isLike) }
    val likeCount = remember { mutableIntStateOf(commentToPost.cmtLikeCount) }

    val isCollapsed = remember { mutableStateOf(true) }

    Column(modifier = Modifier.fillMaxWidth()) {
        Column(
            modifier =
            Modifier
                .fillMaxWidth()
                .clickable {
                    createCommentViewModel.updateReplyUserName("")
                    createCommentViewModel.updateCommentingState(true)
                    createCommentViewModel.updateCmtCmtId(commentToPost.cmtId)
                    createCommentViewModel.updateCmtType(CommentType.CommentToComment)
                }
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.Bottom,
            ) {
                Row(verticalAlignment = Alignment.Bottom) {
                    Text(
                        text = commentToPost.cmtName + if (userName == commentToPost.cmtName) "(我)" else "",
                        fontSize = 16.sp,
                        color = mediumDarkGray
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = timeText,
                        fontSize = 14.sp,
                        color = Color.Gray,
                    )
                }
                Row(horizontalArrangement = Arrangement.End, verticalAlignment = Alignment.Bottom) {
                    Text(text = likeCount.intValue.toString())
                    Spacer(modifier = Modifier.width(4.dp))
                    IconButton(
                        onClick = {
                            scope.launch {
                                userId?.let {
                                    if (isLike.value && DataRepository.unlikeCmt(
                                            jwlToken,
                                            commentToPost.cmtId,
                                            it
                                        ) ||
                                        (!isLike.value && DataRepository.likeCmt(
                                            jwlToken,
                                            commentToPost.cmtId,
                                            it
                                        ))
                                    ) {
                                        likeCount.intValue += if (isLike.value) -1 else 1
                                        isLike.value = !isLike.value
                                        commentToPost.isLike = isLike.value
                                        commentToPost.cmtLikeCount = likeCount.intValue
                                    }
                                }
                            }
                        },
                        modifier = Modifier
                            .align(Alignment.Bottom)
                            .size(18.dp)
                    ) {
                        Icon(
                            imageVector = if (isLike.value) Icons.Default.ThumbUp else Icons.Default.ThumbUpOffAlt,
                            contentDescription = "like",
                            modifier = Modifier.size(16.dp),
                            tint = iconBlue
                        )
                    }
                }
            }
            Spacer(modifier = Modifier.height(4.dp))
            Text(
                text = commentToPost.cmtContent,
                fontSize = 16.sp,
                modifier = Modifier.padding(horizontal = 16.dp)
            )
        }
        Spacer(modifier = Modifier.height(4.dp))
        if (commentToPost.replyList.size == 1 || (commentToPost.replyList.size > 1 && !isCollapsed.value)) {
            commentToPost.replyList.forEach { item -> ReplyCard(item, userName) }
        }
        if (commentToPost.replyList.size > 1) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(horizontal = 16.dp)
                    .clickable { isCollapsed.value = !isCollapsed.value }
            ) {
                Text(
                    text = if (isCollapsed.value) "展开"+commentToPost.replyList.size.toString()+"条回复" else "收起",
                    color = iconBlue,
                    fontSize = 12.sp
                )
            }
        }
        HorizontalDivider(
            modifier = Modifier.padding(vertical = 4.dp),
            color = Color.Gray,
            thickness = 1.dp
        )
    }
}
